/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tiberio.movil.utility.gson;

import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.tiberio.movil.entity.CityPollEntity;
import com.tiberio.movil.entity.ForoPollEntity;
import com.tiberio.movil.entity.PollEntity;
import java.util.ArrayList;
import java.util.Map.Entry;

/**
 *
 * @author chema
 */
public class GsonUtility {
    
    
    
    public static ArrayList<PollEntity> parsearPoll ( String json)
    {
        Gson gson = new Gson();
        ArrayList<PollEntity> pollArray = new  ArrayList<PollEntity>();
        JsonParser parser = new JsonParser();
        JsonArray jsonArray = parser.parse(json).getAsJsonArray();
        
        for(JsonElement elem : jsonArray)
        {
           try{
            String tipo = elem.getAsJsonObject().entrySet().iterator().next().getValue().getAsJsonObject().entrySet().iterator().next().getValue().getAsString();
            
            if("FORUM".equals(tipo))
                 pollArray.add(gson.fromJson(elem, ForoPollEntity.class));
            else if("CITIES".equals(tipo))
                 pollArray.add(gson.fromJson(elem, CityPollEntity.class));
           }
           catch(Exception e){}
            
            /*try
            {
                pollArray.add(gson.fromJson(elem, ForoPollEntity.class));
                continue;
            }
            catch(Throwable t){
                System.out.println("no se puede parsear a el ForoPollEntity elemteo de tipo " +elem.toString()+" ");
                System.out.println(t);
                
            }
            
            try{
                pollArray.add(gson.fromJson(elem, CityPollEntity.class));
                continue;
            }
            catch(Throwable t){
                System.out.println("no se puede parsear a el CityPollEntity elemteo de tipo " +elem.toString()+" ");
                System.out.println(t);
                
            }*/
            
            
            
            
        }
        
        return pollArray;
        
       
    }

    public static String toJson(ArrayList<String> args) {
       
         String datosjson = "{";
         String valores="";
        
        if(args!=null)
        {
            Log.d("", "Entramos en el blucle");
            for (int i = 0; i < (args.size()-2); i = i + 2)
            {
                
                if(!esEntero(args.get(i)))
                    valores += valores +"\""+args.get(i) +"\":"
                                +"\""+args.get(i+1) +"\",";
                else
                    valores += valores +"\""+args.get(i) +"\":"
                                +args.get(i+1)+",";
                datosjson += valores;
                valores ="";
                
                
            }
            
            Log.d("", "Salimos del bucle");
            valores = "";
             if(!esEntero(args.get(args.size()-2)))

                valores += valores +"\""+args.get(args.size()-2) +"\":"
                                 +"\""+args.get(args.size()-1) +"\"";
             else
                
                 valores += valores +"\""+args.get(args.size()-2) +"\":"
                                 +args.get(args.size()-1);
            datosjson += valores;
        }
        
        
        datosjson+= "}";
        
        return datosjson;
    }
    
    
    private static boolean esEntero(String arg ) {
        
        boolean esentero =("sequenceid".equals(arg) || "requestid".equals(arg) || "version".equals(arg)) || "refId".equals(arg) 
                        || "cityid".equals(arg) || "posX".equals(arg) || "posY".equals(arg);
        
        Log.d("", "Es "+ arg+ " un entero?" + esentero );
        
        
        return esentero;
    }
    
}
